package dao;

import common.Group;
import common.User;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class UpdateInformation {
    public boolean updateInformation(User us) {
        Connection conn=null;
        ResultSet res=null;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            Class.forName(Driver);
            //System.out.println("dd"+us.getGender());
            conn= DriverManager.getConnection(url,user,password);
            String sql="update users set username='"+us.getName()+"',gender='"+us.getGender()+"',address='"+us.getAddress()+"',birth='"+us.getBirth()+"',sign='"+us.getSign()+"',headimage='"+us.getHeadimage()+"' where userid='"+us.getId()+"'";
            PreparedStatement presta = conn.prepareStatement(sql);
            int f=0;
            f=presta.executeUpdate();
            if(f!=0){
                return true;
            }else{
                return false;
            }

        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateGroupInformation(Group gp) {
        Connection conn=null;
        ResultSet res=null;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            Class.forName(Driver);
            System.out.println("dd"+gp.getGroupid());
            conn= DriverManager.getConnection(url,user,password);
            String sql="update groupp set group_name='"+gp.getGroupname()+"',group_photo='"+gp.getGroupheadimage()+"',group_introduction='"+gp.getGroupsign()+"'where group_id='"+gp.getGroupid()+"'";
            PreparedStatement presta = conn.prepareStatement(sql);
            int f=0;
            f=presta.executeUpdate();
            if(f!=0){
                return true;
            }else{
                return false;
            }
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
}
